package Model.DAO;

import Model.Entity.Commodity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CommodityDao extends BaseDao{
    public CommodityDao(){
        super("commodity");
    }
    //添加新商品
    public boolean insertCommodity(Commodity commodity)
    {
        commodity.setId(EntityCount());
        return insert(commodity);
    }
    //商品删除
    public boolean deleteCommodity(Long id){
        return delete(id);
    }
    //修改商品属性
    public boolean updateCommodity(Commodity commodity){
        return update(commodity);
    }
    public Commodity searchCommodity(Long id){
        return (Commodity) find(id);
    }
    public ArrayList<Commodity> searchCommodityByCategory(String category)
    {
        Connection conn = null;
        try{
            ArrayList<Commodity> target = new ArrayList<>();
            conn = dataSource.getConnection();
            PreparedStatement pre = conn.prepareStatement("SELECT * FROM commodity WHERE category ='"+ category +"'");
            ResultSet rst = pre.executeQuery();
            while(rst.next())
            {
                Commodity commodity = new Commodity();
                commodity.setId(Long.parseLong(rst.getObject(1).toString()));
                commodity.setName(rst.getObject(2).toString());
                commodity.setRealprice((Double)(rst.getObject(3)));
                commodity.setLocal(rst.getString(4));
                commodity.setSellprice((Double)(rst.getObject(5)));
                commodity.setDescription(rst.getString(6));
                commodity.setActivityid(rst.getLong(7));
                commodity.setJoindate(rst.getDate(9));
                target.add(commodity);
            }
            conn.close();
            return target;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            close(conn);
            return null;
        }
    }

    //查询商品列表
    public ArrayList<Commodity> searchCommodityByName(String name)
    {
        Connection conn = null;
        try{
            ArrayList<Commodity> target = new ArrayList<>();
            conn = dataSource.getConnection();
            PreparedStatement pre = conn.prepareStatement("SELECT * FROM commodity WHERE name like'%"+ name +"%'");
            ResultSet rst = pre.executeQuery();
            while(rst.next())
            {
                Commodity commodity = new Commodity();
                commodity.setId(Long.parseLong(rst.getObject(1).toString()));
                commodity.setName(rst.getObject(2).toString());
                commodity.setRealprice((Double)(rst.getObject(3)));
                commodity.setLocal(rst.getString(4));
                commodity.setSellprice((Double)(rst.getObject(5)));
                commodity.setDescription(rst.getString(6));
                commodity.setActivityid(rst.getLong(7));
                target.add(commodity);
            }
            conn.close();
            return target;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            close(conn);
            return null;
        }
    }
}
